﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Collections;

using System.Globalization;

namespace System
{
    public class Dal : IDal
    {
        private Database _DB;
        private string _ConfigName;
       
        public virtual int TimeOut { get; set; }
        public virtual DBType DBType { get; set; }
        public virtual LogUserInfo LogUser { get; set; }
        public virtual DbTransaction Transaction { get; set; }

        public Dal(string configName)
            : this()
        {
            ConfigName = configName;
        }
   
        public Dal()
        {
            TimeOut = 60;
            DBType = DBType.Sql;
        }

        #region IDAL 成员
      
        public virtual Database DB
        {
            get
            {
                if (_DB == null)
                {
                    if (string.IsNullOrEmpty(ConfigName))
                    {
                        _DB = DatabaseFactory.CreateDatabase();
                    }
                    else
                    {
                        _DB = DatabaseFactory.CreateDatabase(ConfigName);
                    }
                }
                return _DB;
            }

            set
            {
                _DB = value;
            }
        }  
        public virtual string ConfigName
        {
            get
            {
                return _ConfigName;
            }
            set
            {
                if (_ConfigName != value)
                {
                    _DB = null;
                    _ConfigName = value;
                }
            }
        }     

        #endregion

        #region IExecute 成员

        #region ExecuteNonQuery

        /// <summary>
        /// 执行SQL语句，并返回影响的行数
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <returns>返回影响的行数</returns>
        public virtual int ExecuteNonQuery(string commandText)
        {
            return ExecuteNonQuery(commandText, CommandType.Text, null);
        }

        /// <summary>
        /// 执行SQL语句或存储过程，并返回影响的行数
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <param name="commandType">命令字符串类型</param>
        /// <returns>返回影响的行数</returns>
        public virtual int ExecuteNonQuery(string commandText, CommandType commandType)
        {
            return ExecuteNonQuery(commandText, commandType, null);
        }

        /// <summary>
        /// 执行SQL语句，并返回影响的行数
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回影响的行数</returns>
        public virtual int ExecuteNonQuery(string commandText, List<DbParameter> parameters)
        {
            return ExecuteNonQuery(commandText, CommandType.Text, parameters);
        }

        /// <summary>
        /// 执行SQL语句或存储过程，并返回影响的行数
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <param name="commandType">命令字符串类型</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回影响的行数</returns>
        public virtual int ExecuteNonQuery(string commandText, CommandType commandType, List<DbParameter> parameters)
        {
            Database db = DB;
            DbCommand dbCommand = null;
            int result = 0;

            InitDatabase<DbParameter>(commandText, commandType, db, ref dbCommand, parameters);

            if (this.Transaction == null)
            {
                using (dbCommand)
                {
                    result = db.ExecuteNonQuery(dbCommand);
                    GetParameterResultValue(db, dbCommand, parameters);
                    dbCommand.Dispose();
                }
            }
            else
            {
                result = db.ExecuteNonQuery(dbCommand, this.Transaction);
                GetParameterResultValue(db, dbCommand, parameters);
            }

            return result;

        }

        #endregion ExecuteNonQuery

        #region ExecuteScalar

        /// <summary>
        /// 执行SQL语句，并返回第一行第一列的值
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <returns>返回第一行第一列的值</returns>
        public virtual object ExecuteScalar(string commandText)
        {
            return ExecuteScalar(commandText, CommandType.Text, null);
        }

        /// <summary>
        /// 执行SQL语句或存储过程，并返回第一行第一列的值
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <param name="commandType">命令字符串类型</param>
        /// <returns>返回第一行第一列的值</returns>
        public virtual object ExecuteScalar(string commandText, CommandType commandType)
        {
            return ExecuteScalar(commandText, commandType, null);
        }

        /// <summary>
        /// 执行SQL语句，并返回第一行第一列的值
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回第一行第一列的值</returns>
        public virtual object ExecuteScalar(string commandText, List<DbParameter> parameters)
        {
            return ExecuteScalar(commandText, CommandType.Text, parameters);
        }

        /// <summary>
        /// 执行SQL语句或存储过程，并返回第一行第一列的值
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <param name="commandType">命令字符串类型</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回第一行第一列的值</returns>
        public virtual object ExecuteScalar(string commandText, CommandType commandType, List<DbParameter> parameters)
        {
            Database db = DB;
            DbCommand dbCommand = null;
            object result;

            InitDatabase<DbParameter>(commandText, commandType, db, ref dbCommand, parameters);

            if (this.Transaction == null)
            {
                using (dbCommand)
                {
                    result = db.ExecuteScalar(dbCommand);
                    GetParameterResultValue(db, dbCommand, parameters);
                    dbCommand.Dispose();
                }
            }
            else
            {
                result = db.ExecuteScalar(dbCommand, this.Transaction);
                GetParameterResultValue(db, dbCommand, parameters);
            }

            return result;
        }

        #endregion ExecuteScalar

        #region ExecuteReader

        /// <summary>
        /// 执行SQL语句，并返回IDataReader
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <returns>返回IDataReader</returns>
        public virtual IDataReader ExecuteReader(string commandText)
        {
            return ExecuteReader(commandText, CommandType.Text, null);
        }

        /// <summary>
        /// 执行SQL语句或存储过程，并返回IDataReader
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <param name="commandType">命令字符串类型</param>
        /// <returns>返回IDataReader</returns>
        public virtual IDataReader ExecuteReader(string commandText, CommandType commandType)
        {
            return ExecuteReader(commandText, commandType, null);
        }

        /// <summary>
        /// 执行SQL语句，并返回IDataReader
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回IDataReader</returns>
        public virtual IDataReader ExecuteReader(string commandText, List<DbParameter> parameters)
        {
            return ExecuteReader(commandText, CommandType.Text, parameters);
        }

        /// <summary>
        /// 执行SQL语句或存储过程，并返回IDataReader
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <param name="commandType">命令字符串类型</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回IDataReader</returns>
        public virtual IDataReader ExecuteReader(string commandText, CommandType commandType, List<DbParameter> parameters)
        {
            Database db = DB;
            DbCommand dbCommand = null;
            IDataReader result;

            InitDatabase<DbParameter>(commandText, commandType, db, ref dbCommand, parameters);

            if (this.Transaction == null)
            {
                using (dbCommand)
                {
                    result = db.ExecuteReader(dbCommand);
                    //GetParameterResultValue(db, dbCommand, parameters);
                    dbCommand.Dispose();
                }
            }
            else
            {
                result = db.ExecuteReader(dbCommand, this.Transaction);
                //GetParameterResultValue(db, dbCommand, parameters);
            }

            return result;
        }

        #endregion ExecuteReader

        #region ExecuteDataSet


        /// <summary>
        /// 执行SQL语句，并返回DataSet
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <returns>返回DataSet</returns>
        public virtual DataSet ExecuteDataSet(string commandText)
        {
            return ExecuteDataSet(commandText, CommandType.Text, null);
        }

        /// <summary>
        /// 执行SQL语句或存储过程，并返回DataSet
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <param name="commandType">命令字符串类型</param>
        /// <returns>返回DataSet</returns>
        public virtual DataSet ExecuteDataSet(string commandText, CommandType commandType)
        {
            return ExecuteDataSet(commandText, commandType, null);
        }

        /// <summary>
        /// 执行SQL语句，并返回DataSet
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回DataSet</returns>
        public virtual DataSet ExecuteDataSet(string commandText, List<DbParameter> parameters)
        {
            return ExecuteDataSet(commandText, CommandType.Text, parameters);
        }

        /// <summary>
        /// 执行SQL语句或存储过程，并返回DataSet
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <param name="commandType">命令字符串类型</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回DataSet</returns>
        public virtual DataSet ExecuteDataSet(string commandText, CommandType commandType, List<DbParameter> parameters)
        {
            Database db = DB;
            DbCommand dbCommand = null;
            DataSet result;

            InitDatabase<DbParameter>(commandText, commandType, db, ref dbCommand, parameters);

            if (this.Transaction == null)
            {
                using (dbCommand)
                {
                    result = db.ExecuteDataSet(dbCommand);
                    GetParameterResultValue(db, dbCommand, parameters);
                    dbCommand.Dispose();
                }
            }
            else
            {
                result = db.ExecuteDataSet(dbCommand, this.Transaction);
                GetParameterResultValue(db, dbCommand, parameters);
            }

            return result;
        }

        #endregion ExecuteDataSet

        #region 扩展方法

        /// <summary>
        /// 执行SQL语句或存储过程，并返回所影响的行数
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <param name="parameters">参数集合</param>
        /// <param name="commandType">命令字符串类型</param>
        /// <returns>返回所影响的行数</returns>
        public virtual int ExecuteNonQuery(string commandText, List<DBParameter> parameters, CommandType commandType)
        {
            Database db = DB;
            DbCommand dbCommand = null;
            int result = 0;

            InitDatabase<DBParameter>(commandText, commandType, db, ref dbCommand, parameters);

            if (this.Transaction == null)
            {
                using (dbCommand)
                {

                    result = db.ExecuteNonQuery(dbCommand);
                    GetParameterResultValue(db, dbCommand, parameters);
                    dbCommand.Dispose();
                }
            }
            else
            {
                result = db.ExecuteNonQuery(dbCommand, this.Transaction);
                GetParameterResultValue(db, dbCommand, parameters);

            }

            return result;

        }

        /// <summary>
        /// 执行SQL语句或存储过程,并返回第一行第一列的值
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <param name="parameters">参数集合</param>
        /// <param name="commandType">命令字符串类型</param>
        /// <returns>返回第一行第一列的值</returns>
        public virtual object ExecuteScalar(string commandText, List<DBParameter> parameters, CommandType commandType)
        {
            Database db = DB;
            DbCommand dbCommand = null;
            object result;

            InitDatabase<DBParameter>(commandText, commandType, db, ref dbCommand, parameters);

            if (this.Transaction == null)
            {
                using (dbCommand)
                {
                    result = db.ExecuteScalar(dbCommand);
                    GetParameterResultValue(db, dbCommand, parameters);
                    dbCommand.Dispose();
                }
            }
            else
            {
                result = db.ExecuteScalar(dbCommand, this.Transaction);
                GetParameterResultValue(db, dbCommand, parameters);
            }

            return result;
        }

        /// <summary>
        /// 执行SQL语句或存储过程,并返回IDataReader
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <param name="parameters">参数集合</param>
        /// <param name="commandType">命令字符串类型</param>
        /// <returns>返回IDataReader</returns>
        public virtual IDataReader ExecuteReader(string commandText, List<DBParameter> parameters, CommandType commandType)
        {
            Database db = DB;
            DbCommand dbCommand = null;
            IDataReader result;

            InitDatabase<DBParameter>(commandText, commandType, db, ref dbCommand, parameters);

            if (this.Transaction == null)
            {
                using (dbCommand)
                {
                    result = db.ExecuteReader(dbCommand);
                    // GetParameterResultValue(db, dbCommand, parameters);
                    dbCommand.Dispose();

                }
            }
            else
            {
                result = db.ExecuteReader(dbCommand, this.Transaction);
                //GetParameterResultValue(db, dbCommand, parameters);
            }

            return result;
        }

        /// <summary>
        /// 执行SQL语句或存储过程,并返回DataSet
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <param name="parameters">参数集合</param>
        /// <param name="commandType">命令字符串类型</param>
        /// <returns>返回DataSet</returns>
        public virtual DataSet ExecuteDataSet(string commandText, List<DBParameter> parameters, CommandType commandType)
        {
            Database db = DB;
            DbCommand dbCommand = null;
            DataSet result;

            InitDatabase<DBParameter>(commandText, commandType, db, ref dbCommand, parameters);

            if (this.Transaction == null)
            {
                using (dbCommand)
                {

                    result = db.ExecuteDataSet(dbCommand);
                    GetParameterResultValue(db, dbCommand, parameters);
                    dbCommand.Dispose();
                }
            }
            else
            {
                result = db.ExecuteDataSet(dbCommand, this.Transaction);
                GetParameterResultValue(db, dbCommand, parameters);
            }

            return result;
        }

        /// <summary>
        /// 执行SQL语句，并返回所影响的行数
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <returns>返回所影响的行数</returns>
        public virtual int ExecuteNonQuery(List<DBParameter> parameters, string commandText)
        {
            return ExecuteNonQuery(commandText, parameters, CommandType.Text);
        }

        /// <summary>
        /// 执行SQL语句，并返回第一行第一列的值
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <returns>返回第一行第一列的值</returns>
        public virtual object ExecuteScalar(List<DBParameter> parameters, string commandText)
        {
            return ExecuteScalar(commandText, parameters, CommandType.Text);
        }

        /// <summary>
        ///  执行SQL语句，并返回IDataReader
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <returns>返回IDataReader</returns>
        public virtual IDataReader ExecuteReader(List<DBParameter> parameters, string commandText)
        {
            return ExecuteReader(commandText, parameters, CommandType.Text);
        }

        /// <summary>
        /// 执行SQL语句，并返回DataSet
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <returns>返回DataSet</returns>
        public virtual DataSet ExecuteDataSet(List<DBParameter> parameters, string commandText)
        {
            return ExecuteDataSet(commandText, parameters, CommandType.Text);
        }

        #endregion

        #endregion

        #region IDisposable 成员

        private bool disposed;

        /// <summary>
        /// 释放资源
        /// </summary>
        public virtual void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    if (this.Transaction != null)
                    {
                        //try
                        //{
                        this.Transaction.Dispose();
                        this.Transaction = null;
                        //}
                        //catch 
                        //{
                        //}                    
                    }

                }
            }
            disposed = true;
        }

        ~Dal()
        {
            Dispose(false);
        }

        #endregion

        #region IDALData 成员

        #region 基本

        /// <summary>
        /// 开始事务以执行数据库操作
        /// </summary>
        public virtual void OpenTransaction()
        {
            if (Transaction == null)
            {
                DbConnection dbConnection = DB.CreateConnection();
                dbConnection.Open();
                Transaction = dbConnection.BeginTransaction();
            }

        }

        /// <summary>
        /// 判断是否存在符合条件的数据
        /// </summary>
        /// <param name="filter">筛选条件</param>
        /// <param name="tableName">表名</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回是否存在符合条件的数据</returns>
        public virtual bool Exists(string filter, string tableName, List<DBParameter> parameters)
        {
            string sql = SqlHelper.GetListSql("COUNT(1)", filter, tableName, this.DBType);

            object obj = this.ExecuteScalar(parameters, sql);

            return DataHelper.IsDBNull(obj);
        }

        /// <summary>
        /// 查询符合条件的记录数
        /// </summary>
        /// <param name="filter">筛选条件</param>
        /// <param name="tableName">表名</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回符合条件的记录数</returns>
        public virtual int GetCount(string filter, string tableName, List<DBParameter> parameters)
        {
            string sql = SqlHelper.GetListSql("COUNT(0)", filter, tableName, this.DBType);

            object obj = this.ExecuteScalar(parameters, sql);

            return int.Parse(obj.ToString(), CultureInfo.InvariantCulture);
        }

        /// <summary>
        ///  判断是否存在符合条件的数据
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回是否存在符合条件的数据</returns>
        public virtual bool Exists(List<DbParameter> parameters, string filter, string tableName)
        {
            string sql = SqlHelper.GetListSql("COUNT(1)", filter, tableName, this.DBType);

            object obj = this.ExecuteScalar(sql, parameters);

            return DataHelper.IsDBNull(obj);
        }

        /// <summary>
        /// 查询符合条件的记录数
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回符合条件的记录数</returns>
        public virtual int GetCount(List<DbParameter> parameters, string filter, string tableName)
        {
            string sql = SqlHelper.GetListSql("COUNT(0)", filter, tableName, this.DBType);

            object obj = this.ExecuteScalar(sql, parameters);

            return int.Parse(obj.ToString(), CultureInfo.InvariantCulture);
        }

        #endregion

        #region Delete

        /// <summary>
        /// 删除符合条件的记录
        /// </summary>
        /// <param name="filter">筛选条件</param>
        /// <param name="tableName">表名</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回影响的记录数</returns>
        public virtual int Delete(string filter, string tableName, List<DBParameter> parameters)
        {
            string sql = SqlHelper.GetDeleteSql(filter, tableName, this.DBType);
            return ExecuteNonQuery(parameters, sql);
        }

        /// <summary>
        /// 删除指定的对象
        /// </summary>
        /// <param name="model">待处理对象</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Delete(BaseModel model, string tableName)
        {
            DBParameter fieldInfo = null;
            string filter = SqlHelper.GetDefaultFilter(model, this.DBType, ref fieldInfo);
            List<DBParameter> fieldInfos = new List<DBParameter>();
            fieldInfos.Add(fieldInfo);

            return Delete(filter, tableName, fieldInfos);
        }

        /// <summary>
        /// 删除符合主键的记录
        /// </summary>
        /// <param name="primaryKeyInfo">主键信息</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Delete(DBParameter primaryKeyInfo, string tableName)
        {
            List<DBParameter> parameters = new List<DBParameter>();
            parameters.Add(primaryKeyInfo);
            return Delete(parameters, tableName);
        }

        /// <summary>
        /// 删除符合参数条件的记录
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Delete(List<DBParameter> parameters, string tableName)
        {
            string filter = SqlHelper.GetDefaultFilter(parameters, this.DBType);
            return Delete(filter, tableName, parameters);
        }

        /// <summary>
        /// 删除集合中的对象
        /// </summary>
        /// <param name="list">待处理对象集合</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Delete(ArrayList list, string tableName)
        {
            int result = list.Count;
            if (Transaction == null)
            {
                this.OpenTransaction();
                try
                {
                    foreach (BaseModel model in list)
                    {
                        this.Delete(model, tableName);
                    }
                    this.Transaction.Commit();
                }
                catch
                {
                    this.Transaction.Rollback();
                    result = 0;
                }
                finally
                {
                    this.Transaction.Dispose();
                    this.Transaction = null;
                }
            }
            else
            {
                foreach (BaseModel model in list)
                {
                    this.Delete(model, tableName);
                }
            }

            return result;
        }

        /// <summary>
        /// 删除符合条件的记录
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Delete(List<DbParameter> parameters, string filter, string tableName)
        {
            string sql = SqlHelper.GetDeleteSql(filter, tableName, this.DBType);
            return ExecuteNonQuery(sql, parameters);
        }

        /// <summary>
        /// 删除符合参数条件的记录
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Delete(string tableName, List<DbParameter> parameters)
        {
            string filter = SqlHelper.GetDefaultFilter(this.DBType, parameters);
            return Delete(parameters, filter, tableName);
        }

        #endregion

        #region Update

        /// <summary>
        /// 更新符合条件的记录
        /// </summary>
        /// <param name="fieldName">更新字段名</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="tableName">表名</param>
        /// <param name="value">更新后的值</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Update(string fieldName, string filter, string tableName, object value, List<DBParameter> parameters)
        {
            DBParameter parameter = null;
            string sql = SqlHelper.GetUpdateSql(ref parameter, fieldName, filter, tableName, value, this.DBType);

            if (parameters == null)
            {
                parameters = new List<DBParameter>();
            }

            parameters.Add(parameter);

            return ExecuteNonQuery(parameters, sql);
        }

        /// <summary>
        /// 更新对象对应的记录
        /// </summary>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Update(BaseModel model, string fields, string tableName)
        {
            DBParameter parameter = null;
            string filter = SqlHelper.GetDefaultFilter(model, this.DBType, ref parameter);

            List<DBParameter> parameters = new List<DBParameter>();
            parameters.Add(parameter);

            return Update(model, fields, filter, tableName, parameters);
        }

        /// <summary>
        /// 更新对象列表所对应的记录
        /// </summary>
        /// <param name="list">待处理对象集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Update(ArrayList list, string fields, string tableName)
        {
            int result = list.Count;
            if (Transaction == null)
            {
                this.OpenTransaction();
                try
                {
                    foreach (BaseModel model in list)
                    {
                        this.Update(model, fields, tableName);
                    }
                    this.Transaction.Commit();
                }
                catch
                {
                    this.Transaction.Rollback();
                    result = 0;
                }
                finally
                {
                    this.Transaction.Dispose();
                    this.Transaction = null;
                }
            }
            else
            {
                foreach (BaseModel model in list)
                {
                    this.Update(model, fields, tableName);
                }
            }
            return result;
        }

        /// <summary>
        /// 更新符合条件的记录为对象中的值
        /// </summary>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="tableName">表名</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Update(BaseModel model, string fields, string filter, string tableName, List<DBParameter> parameters)
        {
            model.OnSubmiting();

            string sql = SqlHelper.GetUpdateSql(model, fields, filter, tableName, parameters, this.DBType);

            int result = 0;

            if (!string.IsNullOrEmpty(sql))
                result = ExecuteNonQuery(parameters, sql);

            model.OnSubmited();

            return result;
        }

        /// <summary>
        /// 更新符合条件的记录
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="fieldName">需要更新的记录字段名</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="tableName">表名</param>
        /// <param name="value">更新后的值</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Update(List<DbParameter> parameters, string fieldName, string filter, string tableName, object value)
        {
            return Update(fieldName, filter, tableName, value, SqlHelper.ConvertDbParameters(parameters));
        }

        /// <summary>
        /// 更新符合条件的记录数为指定对象里的值
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Update(List<DbParameter> parameters, BaseModel model, string fields, string filter, string tableName)
        {
            return Update(model, fields, filter, tableName, SqlHelper.ConvertDbParameters(parameters));
        }

        #endregion Update

        #region Insert

        /// <summary>
        /// 添加指定对象
        /// </summary>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Insert(BaseModel model, string fields, string tableName)
        {
            model.OnSubmiting();

            List<DBParameter> parameters = new List<DBParameter>();

            string sql = SqlHelper.GetAddSql(model, fields, tableName, parameters, this.DBType);

            int result = 0;
            if (!string.IsNullOrEmpty(sql))
                result = ExecuteNonQuery(parameters, sql);

            model.OnSubmited();

            return result;
        }

        /// <summary>
        /// 添加集合
        /// </summary>
        /// <param name="list">待处理对象集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Insert(ArrayList list, string fields, string tableName)
        {
            int result = list.Count;

            if (Transaction == null)
            {
                this.OpenTransaction();
                try
                {
                    foreach (BaseModel model in list)
                    {
                        this.Insert(model, fields, tableName);
                    }
                    this.Transaction.Commit();
                }
                catch
                {
                    this.Transaction.Rollback();
                    result = 0;
                }
                finally
                {
                    this.Transaction.Dispose();
                    this.Transaction = null;
                }
            }
            else
            {
                foreach (BaseModel model in list)
                {
                    this.Insert(model, fields, tableName);
                }
            }
            return result;
        }

        /// <summary>
        /// 添加或更新指定对象，若对象已存在则更新该对象，若不存在，则添加该对象
        /// </summary>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int InsertOrUpdate(BaseModel model, string fields, string tableName)
        {
            model.OnSubmiting();

            List<DBParameter> parameters = new List<DBParameter>();

            string sql = "";

            if (model.GetIsNew())
            {
                sql = SqlHelper.GetAddSql(model, fields, tableName, parameters, this.DBType);
            }
            else
            {
                sql = SqlHelper.GetUpdateSql(model, fields, tableName, parameters, this.DBType);
            }

            //    string sql = SqlHelper.GetInsertOrUpdateSql(model, fields, tableName, parameters, this.DBType);

            int result = 0;

            if (!string.IsNullOrEmpty(sql))
                result = ExecuteNonQuery(parameters, sql);

            model.OnSubmited();

            return result;

        }

        /// <summary>
        /// 添加或更新指定对象集合，若对象已存在则更新该对象，若不存在，则添加该对象
        /// </summary>
        /// <param name="list">待处理对象集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int InsertOrUpdate(ArrayList list, string fields, string tableName)
        {
            int result = list.Count;
            if (Transaction == null)
            {
                this.OpenTransaction();
                try
                {
                    foreach (BaseModel model in list)
                    {
                        this.InsertOrUpdate(model, fields, tableName);
                    }
                    this.Transaction.Commit();
                }
                catch
                {
                    this.Transaction.Rollback();
                    result = 0;
                }
                finally
                {
                    this.Transaction.Dispose();
                    this.Transaction = null;
                }
            }
            else
            {
                foreach (BaseModel model in list)
                {
                    this.InsertOrUpdate(model, fields, tableName);
                }
            }
            return result;
        }

        #endregion

        #region GetDataTable

        /// <summary>
        /// 执行SQL查询，并返回DataTable结果
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>返回DataTable</returns>
        public virtual DataTable GetDataTable(string sql)
        {
            return GetDataTable(sql, null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual DataTable GetDataTable(string sql, List<DBParameter> parameters)
        {
            return ExecuteDataSet(parameters, sql).Tables[0];
        }

        /// <summary>
        /// 执行SQL查询，并返回DataTable结果
        /// </summary>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="tableName">表名</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回DataTable</returns>
        public virtual DataTable GetDataTable(string fields, string filter, string sorts, string tableName, List<DBParameter> parameters)
        {
            filter = BaseHelper.GetDefaultValue(filter, "1=1");

            fields = BaseHelper.GetDefaultValue(fields, "*");
            string sql = SqlHelper.FormatSql(fields, filter, sorts, tableName, this.DBType);

            return GetDataTable(sql, parameters);
        }

        /// <summary>
        /// 以分页形式返回符合条件的DataTable结果
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="recordCount">总记录数。为-1时，将从数据库获取该值</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="tableName">表名</param>
        /// <param name="primaryKey">主键名</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回DataTable</returns>
        public virtual DataTable GetDataTable(int? pageIndex, int? pageSize, ref int? recordCount, string fields, string filter, string sorts, string tableName, string primaryKey, List<DBParameter> parameters)
        {
            SqlHelper.FormatArg(ref pageSize, ref pageIndex, ref recordCount, ref fields, ref filter, ref sorts, primaryKey);

            DataTable dt = GetDataSet(pageIndex, pageSize, ref recordCount, fields, filter, sorts, tableName, primaryKey, parameters).Tables[0];

            return dt;
        }

        /// <summary>
        /// 执行SQL查询，返回结果DataTable
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="sql">SQL语句</param>
        /// <returns>返回结果DataTable</returns>
        public virtual DataTable GetDataTable(List<DbParameter> parameters, string sql)
        {
            return GetDataTable(sql, SqlHelper.ConvertDbParameters(parameters));
        }

        /// <summary>
        /// 返回符合条件的记录到DataTable
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回结果DataTable</returns>
        public virtual DataTable GetDataTable(List<DbParameter> parameters, string fields, string filter, string sorts, string tableName)
        {
            return GetDataTable(fields, filter, sorts, tableName, SqlHelper.ConvertDbParameters(parameters));
        }

        /// <summary>
        /// 以分页形式返回符合条件的结果到DataTable
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="pageIndex">待获取数据的页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="recordCount">总记录数。为-1时，将从数据库获取该值</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="tableName">表名</param>
        /// <param name="primaryKey">主键名</param>
        /// <returns>返回结果DataTable</returns>
        public virtual DataTable GetDataTable(List<DbParameter> parameters, int? pageIndex, int? pageSize, ref int? recordCount, string fields, string filter, string sorts, string tableName, string primaryKey)
        {
            return GetDataTable(pageIndex, pageSize, ref recordCount, fields, filter, sorts, tableName, primaryKey, SqlHelper.ConvertDbParameters(parameters));
        }

        /// <summary>
        /// 以分页形式返回符合条件的结果到DataSet
        /// </summary>
        /// <param name="pageIndex">待获取数据的页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="recordCount">总记录数。为-1时，将从数据库获取该值</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="tableName">表名</param>
        /// <param name="primaryKey">主键名</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回结果DataSet</returns>
        protected virtual DataSet GetDataSet(int? pageIndex, int? pageSize, ref int? recordCount, string fields, string filter, string sorts, string tableName, string primaryKey, List<DBParameter> parameters)
        {
            if (parameters == null) parameters = new List<DBParameter>();

            DBParameter parameter = new DBParameter("", "RecordCount", recordCount);
            parameter.Size = 4;
            parameter.DbType = DbType.Int32;
            parameter.Direction = ParameterDirection.Output;

            parameters.Add(parameter);

            parameters = InitDataParameter(parameters, pageIndex, pageSize);

            DataSet ds = null;

            string sql = SqlHelper.GetPageViewSql(pageIndex, pageSize, fields, filter, sorts, tableName, primaryKey, this.DBType);
            ds = ExecuteDataSet(sql, parameters, CommandType.Text);
            recordCount = Convert.ToInt32(parameter.Value, CultureInfo.InvariantCulture);

            return ds;
        }

        #endregion GetDataTable

        #region IDalData 成员

        /// <summary>
        /// 以分页形式返回符合条件的结果到DataTable
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="pageInfo">封装的分页数据筛选信息</param>
        /// <returns>返回结果DataTable</returns>
        public virtual DataTable GetDataTable(List<DbParameter> parameters, PageInfo pageInfo)
        {
            pageInfo = InitSqlInfo<PageInfo>(pageInfo);
            int? re = pageInfo.RecordCount;

            DataTable dt = GetDataTable(parameters, pageInfo.PageIndex, pageInfo.PageSize, ref re, pageInfo.Fields, pageInfo.Filter, pageInfo.Sorts, pageInfo.TableName, pageInfo.PrimaryKey);

            pageInfo.RecordCount = re.Value;

            return dt;
        }

        /// <summary>
        /// 根据条件返回DataTable结果
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="sqlInfo">封装的筛选信息</param>
        /// <returns>返回结果DataTable</returns>
        public virtual DataTable GetDataTable(List<DbParameter> parameters, SqlInfo sqlInfo)
        {
            sqlInfo = InitSqlInfo<SqlInfo>(sqlInfo);

            return GetDataTable(parameters, sqlInfo.Fields, sqlInfo.Filter, sqlInfo.Sorts, sqlInfo.TableName);
        }

        /// <summary>
        /// 以分页形式返回符合条件的结果到DataTable
        /// </summary>
        /// <param name="pageInfo">封装的分页数据筛选信息</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回结果DataTable</returns>
        public virtual DataTable GetDataTable(PageInfo pageInfo, List<DBParameter> parameters)
        {
            pageInfo = InitSqlInfo<PageInfo>(pageInfo);

            int? re = pageInfo.RecordCount;

            DataTable dt = GetDataTable(pageInfo.PageIndex, pageInfo.PageSize, ref re, pageInfo.Fields, pageInfo.Filter, pageInfo.Sorts, pageInfo.TableName, pageInfo.PrimaryKey, parameters);

            pageInfo.RecordCount = re.Value;

            return dt;
        }

        /// <summary>
        /// 根据条件返回DataTable结果
        /// </summary>
        /// <param name="sqlInfo">封装的筛选信息</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回结果DataTable</returns>
        public virtual DataTable GetDataTable(SqlInfo sqlInfo, List<DBParameter> parameters)
        {
            sqlInfo = InitSqlInfo<SqlInfo>(sqlInfo);

            return GetDataTable(sqlInfo.Fields, sqlInfo.Filter, sqlInfo.Sorts, sqlInfo.TableName, parameters);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sqlInfo">封装的筛选信息</param>
        /// <returns>返回受影响的记录数</returns>
        protected virtual T InitSqlInfo<T>(T sqlInfo) where T : SqlInfo, new()
        {
            if (sqlInfo == null)
                sqlInfo = new T();
            return sqlInfo;
        }

        #endregion

        #region IDalData 成员

        /// <summary>
        /// 删除指定对象
        /// </summary>
        /// <param name="model">待处理对象</param>
        /// <returns>返回影响的记录数</returns>
        public virtual int Delete(BaseModel model)
        {
            if (model == null)
                return 0;
            string tableName = InitTableName(model);
            return this.Delete(model, tableName);
        }

        /// <summary>
        /// 删除指定集合
        /// </summary>
        /// <param name="list">待处理对象集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Delete(ArrayList list)
        {
            if (list == null || list.Count <= 0)
                return 0;
            string tableName = InitTableName((BaseModel)list[0]);
            return this.Delete(list, tableName);
        }

        /// <summary>
        /// 添加指定对象到数据库
        /// </summary>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Insert(BaseModel model, string fields)
        {
            if (model == null)
                return 0;
            string tableName = InitTableName(model);
            return this.Insert(model, fields, tableName);
        }

        /// <summary>
        /// 添加指定对象集合到数据库
        /// </summary>
        /// <param name="list">待处理对象集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Insert(ArrayList list, string fields)
        {
            if (list == null || list.Count <= 0)
                return 0;
            string tableName = InitTableName((BaseModel)list[0]);
            return this.Insert(list, fields, tableName);
        }

        /// <summary>
        /// 添加或更新指定对象，若对象已存在则更新该对象，若不存在，则添加该对象
        /// </summary>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int InsertOrUpdate(BaseModel model, string fields)
        {
            if (model == null)
                return 0;
            string tableName = InitTableName(model);
            return this.InsertOrUpdate(model, fields, tableName);
        }

        /// <summary>
        /// 添加或更新指定对象，若对象已存在则更新该对象，若不存在，则添加该对象
        /// </summary>
        /// <param name="list">待处理对象集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int InsertOrUpdate(ArrayList list, string fields)
        {
            if (list == null || list.Count <= 0)
                return 0;
            string tableName = InitTableName((BaseModel)list[0]);
            return this.InsertOrUpdate(list, fields, tableName);
        }

        /// <summary>
        /// 更新指定对象
        /// </summary>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Update(BaseModel model, string fields)
        {
            if (model == null)
                return 0;
            string tableName = InitTableName(model);
            return this.Update(model, fields, tableName);
        }

        /// <summary>
        /// 更新集合中的对象
        /// </summary>
        /// <param name="list">待处理对象集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Update(ArrayList list, string fields)
        {
            if (list == null || list.Count <= 0)
                return 0;
            string tableName = InitTableName((BaseModel)list[0]);
            return this.Update(list, fields, tableName);
        }

        /// <summary>
        /// 更新集合中的对象
        /// </summary>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Update(BaseModel model, string fields, string filter, List<DBParameter> parameters)
        {
            if (model == null)
                return 0;
            string tableName = InitTableName(model);
            return this.Update(model, fields, filter, tableName, parameters);
        }

        /// <summary>
        /// 更新指定对象
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Update(List<DbParameter> parameters, BaseModel model, string fields, string filter)
        {
            if (model == null)
                return 0;
            string tableName = InitTableName(model);

            return Update(parameters, model, fields, filter, tableName);
        }

        #endregion

        #endregion

        #region ITM 成员

        #region Add

        /// <summary>
        /// 添加泛型对象到数据库中
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int _Add<Tm>(Tm model, string fields) where Tm : BaseModel, new()
        {
            if (model == null)
                return 0;
            string tableName = InitTableName<Tm>();

            return this.Insert(model, fields, tableName);
        }

        /// <summary>
        /// 添加泛型对象集合到数据库中
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="list">待处理对象集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int _Add<Tm>(List<Tm> list, string fields) where Tm : BaseModel, new()
        {
            string tableName = InitTableName<Tm>();

            return _Add<Tm>(list, fields, tableName);
        }

        /// <summary>
        /// 添加泛型对象到数据库中
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int _Add<Tm>(Tm model, string fields, string tableName) where Tm : BaseModel, new()
        {
            if (model == null)
                return 0;
            return this.Insert(model, fields, tableName);
        }

        /// <summary>
        /// 添加泛型对象集合到数据库中
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="list">待处理对象集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int _Add<Tm>(List<Tm> list, string fields, string tableName) where Tm : BaseModel, new()
        {
            int result = list.Count;

            if (Transaction == null)
            {
                this.OpenTransaction();
                try
                {
                    foreach (Tm model in list)
                    {
                        this.Insert(model, fields, tableName);
                    }
                    this.Transaction.Commit();
                }
                catch
                {
                    this.Transaction.Rollback();
                    result = 0;
                }
                finally
                {
                    this.Transaction.Dispose();
                    this.Transaction = null;
                }
            }
            else
            {
                foreach (Tm model in list)
                {
                    this.Insert(model, fields, tableName);
                }
            }
            return result;
        }

        #endregion Add

        #region Remove

        /// <summary>
        /// 从数据库中删除指定对象
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="model">待处理对象</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int _Remove<Tm>(Tm model) where Tm : BaseModel, new()
        {
            if (model == null)
                return 0;
            string tableName = InitTableName<Tm>();
            return this.Delete(model, tableName);
        }

        /// <summary>
        /// 从数据库中删除指定对象集合
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="list">待处理对象集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int _Remove<Tm>(List<Tm> list) where Tm : BaseModel, new()
        {
            string tableName = InitTableName<Tm>();
            return this._Remove<Tm>(list, tableName);
        }

        /// <summary>
        /// 通过对象ID删除对象
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="id">对象ID</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int _Remove<Tm>(string id) where Tm : BaseModel, new()
        {
            string tableName = InitTableName<Tm>();
            return this._Remove<Tm>(id, tableName);
        }

        /// <summary>
        /// 从数据库中删除指定对象
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="model">待处理对象</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int _Remove<Tm>(Tm model, string tableName) where Tm : BaseModel, new()
        {
            return this.Delete(model, tableName);
        }

        /// <summary>
        /// 从数据库中删除指定对象集合
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="list">待处理对象集合</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int _Remove<Tm>(List<Tm> list, string tableName) where Tm : BaseModel, new()
        {
            int result = list.Count;

            if (Transaction == null)
            {
                this.OpenTransaction();
                try
                {
                    foreach (Tm model in list)
                    {
                        this._Remove<Tm>(model, tableName);
                    }
                    this.Transaction.Commit();
                }
                catch
                {
                    this.Transaction.Rollback();
                    result = 0;
                }
                finally
                {
                    this.Transaction.Dispose();
                    this.Transaction = null;
                }
            }
            else
            {
                foreach (Tm model in list)
                {
                    this._Remove<Tm>(model, tableName);
                }
            }
            return result;
        }

        /// <summary>
        /// 通过ID删除对象
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="id">对象ID</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int _Remove<Tm>(string id, string tableName) where Tm : BaseModel, new()
        {
            Tm model = new Tm();
            DBParameter info = new DBParameter(model.GetPrimaryKey(), model.GetPrimaryKey() + "PrimaryKey", id);
            return this.Delete(info, tableName);
        }

        #endregion Remove

        #region Modify

        /// <summary>
        /// 更新对象到数据库
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int _Modify<Tm>(Tm model, string fields) where Tm : BaseModel, new()
        {
            string tableName = InitTableName<Tm>();
            return this._Modify<Tm>(model, fields, tableName);
        }

        /// <summary>
        /// 更新集合中的对象到数据库
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="list">待处理对象集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int _Modify<Tm>(List<Tm> list, string fields) where Tm : BaseModel, new()
        {
            string tableName = InitTableName<Tm>();
            return this._Modify<Tm>(list, fields, tableName);
        }

        /// <summary>
        /// 更新对象到数据库
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int _Modify<Tm>(Tm model, string fields, string tableName) where Tm : BaseModel, new()
        {
            return this.Update(model, fields, tableName);
        }

        /// <summary>
        /// 更新集合中的对象到数据库
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="list">待处理对象集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int _Modify<Tm>(List<Tm> list, string fields, string tableName) where Tm : BaseModel, new()
        {
            int result = list.Count;

            if (Transaction == null)
            {
                this.OpenTransaction();
                try
                {
                    foreach (Tm model in list)
                    {
                        this.Update(model, fields, tableName);
                    }
                    this.Transaction.Commit();
                }
                catch
                {
                    this.Transaction.Rollback();
                    result = 0;
                }
                finally
                {
                    this.Transaction.Dispose();
                    this.Transaction = null;
                }
            }
            else
            {
                foreach (Tm model in list)
                {
                    this.Update(model, fields, tableName);
                }
            }
            return result;
        }

        #endregion

        #region AddOrModify

        /// <summary>
        /// 新增或更新对象到数据库，若对象已存在，则更新，反之，则新增。
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int _AddOrModify<Tm>(Tm model, string fields) where Tm : BaseModel, new()
        {
            string tableName = InitTableName<Tm>();
            return this._AddOrModify<Tm>(model, fields, tableName);
        }

        /// <summary>
        /// 新增或更新集合中的对象到数据库，若对象已存在，则更新，反之，则新增。
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="list">待处理对象集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int _AddOrModify<Tm>(List<Tm> list, string fields) where Tm : BaseModel, new()
        {
            string tableName = InitTableName<Tm>();
            return this._AddOrModify<Tm>(list, fields, tableName);
        }

        /// <summary>
        /// 新增或更新对象到数据库，若对象已存在，则更新，反之，则新增。
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="model">待处理对象</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int _AddOrModify<Tm>(Tm model, string fields, string tableName) where Tm : BaseModel, new()
        {
            return this.InsertOrUpdate(model, fields, tableName);
        }

        /// <summary>
        /// 新增或更新集合中对象到数据库，若对象已存在，则更新，反之，则新增。
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="list">待处理对象集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int _AddOrModify<Tm>(List<Tm> list, string fields, string tableName) where Tm : BaseModel, new()
        {
            int result = list.Count;

            if (Transaction == null)
            {
                this.OpenTransaction();
                try
                {
                    foreach (Tm model in list)
                    {
                        this.InsertOrUpdate(model, fields, tableName);
                    }
                    this.Transaction.Commit();
                }
                catch
                {
                    this.Transaction.Rollback();
                    result = 0;
                }
                finally
                {
                    this.Transaction.Dispose();
                    this.Transaction = null;
                }
            }
            else
            {
                foreach (Tm model in list)
                {
                    this.InsertOrUpdate(model, fields, tableName);
                }
            }
            return result;
        }

        #endregion AddOrModify

        #region GetModel

        /// <summary>
        /// 获取符合条件的第一个对象
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="tableName">表名</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回符合条件的对象</returns>
        public virtual Tm _GetModel<Tm>(string fields, string filter, string sorts, string tableName, List<DBParameter> parameters) where Tm : BaseModel, new()
        {
            filter = BaseHelper.GetDefaultValue(filter, "1=1");
            fields = BaseHelper.GetDefaultValue(fields, "*");
            if (parameters == null && string.IsNullOrEmpty(tableName)) { 
                tableName = InitTableName<Tm>();
            }
            string sql;
            if (string.IsNullOrEmpty(sorts))
                sql = SqlHelper.FormatSql(fields, filter, tableName, DBType);
            else
                sql = SqlHelper.FormatSql(fields, filter, sorts, tableName, DBType);
            return _GetModel<Tm>(sql, parameters);
        }

        /// <summary>
        /// 通过ID获取对象
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="id">对象ID</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回对象</returns>
        public virtual Tm _GetModel<Tm>(string id, string fields, string tableName) where Tm : BaseModel, new()
        {
            Tm model = new Tm();
            string primaryKey = model.GetPrimaryKey();

            fields = BaseHelper.GetDefaultValue(fields, "*");

            string sql = SqlHelper.FormatSql(fields, primaryKey + " =" + SqlHelper.FormatVariable(DBType) + primaryKey + "PrimaryKey", tableName, DBType);

            List<DBParameter> parameters = new List<DBParameter>();
            DBParameter parameter = new DBParameter(primaryKey, primaryKey + "PrimaryKey", id);
            parameters.Add(parameter);

            return _GetModel<Tm>(sql, parameters);
        }

        /// <summary>
        /// 通过参数获取符合条件的对象
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="parameters">参数集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回对象</returns>
        public virtual Tm _GetModel<Tm>(List<DBParameter> parameters, string fields, string tableName) where Tm : BaseModel, new()
        {
            fields = BaseHelper.GetDefaultValue(fields, "*");

            string filter = SqlHelper.GetDefaultFilter(parameters, this.DBType);
            string sql = SqlHelper.FormatSql(filter, fields, tableName, DBType);

            return _GetModel<Tm>(sql, parameters);
        }

        /// <summary>
        /// 获取符合条件的对象
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回对象</returns>
        public virtual Tm _GetModel<Tm>(string fields, string filter, string sorts, List<DBParameter> parameters) where Tm : BaseModel, new()
        {
            string tableName = InitTableName<Tm>();
            return _GetModel<Tm>(fields, filter, sorts, tableName, parameters);
        }

        /// <summary>
        /// 通过ID获取对象
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="id">对象ID</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回对象</returns>
        public virtual Tm _GetModel<Tm>(string id, string fields) where Tm : BaseModel, new()
        {
            string tableName = InitTableName<Tm>();
            return _GetModel<Tm>(id, fields, tableName);
        }
        /// <summary>
        /// 通过ID获取对象
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="id">对象ID</param>     
        /// <returns>返回对象</returns>
        public virtual Tm _GetModel<Tm>(string id) where Tm : BaseModel, new()
        {
            string tableName = InitTableName<Tm>();
            return _GetModel<Tm>(id, "", tableName);
        }
        /// <summary>
        /// 通过参数获取符合条件的对象
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="parameters">参数集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回对象</returns>
        public virtual Tm _GetModel<Tm>(List<DBParameter> parameters, string fields) where Tm : BaseModel, new()
        {
            string tableName = InitTableName<Tm>();
            return _GetModel<Tm>(parameters, fields, tableName);
        }

        /// <summary>
        /// 通过SQL语句获取对象
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回符合条件的对象</returns>
        public virtual Tm _GetModel<Tm>(string sql, List<DBParameter> parameters) where Tm : BaseModel, new()
        {
            Tm model = null;

            using (IDataReader dataReader = ExecuteReader(parameters, sql))
            {
                if (dataReader.Read())
                {
                    model = _GetModel<Tm>(dataReader);
                }
            }
            return model;
        }

        #endregion GetModel

        #region GetList

        /// <summary>
        /// 获取全部对象集合
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <returns>返回对象集合</returns>
        public virtual List<Tm> _GetList<Tm>() where Tm : BaseModel, new()
        {
            string tableName = InitTableName<Tm>();
            return _GetList<Tm>(tableName);
        }

        /// <summary>
        /// 根据SQL语句获取对象集合
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回对象集合</returns>
        public virtual List<Tm> _GetList<Tm>(string sql, List<DBParameter> parameters) where Tm : BaseModel, new()
        {
            IDataReader reader = ExecuteReader(parameters, sql);
            return _GetList<Tm>(reader);
        }

        /// <summary>
        /// 获取符合条件的对象集合
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回对象集合</returns>
        public virtual List<Tm> _GetList<Tm>(string fields, string filter, string sorts, List<DBParameter> parameters) where Tm : BaseModel, new()
        {
            string tableName = InitTableName<Tm>();

            return _GetList<Tm>(fields, filter, sorts, tableName, parameters);
        }

        /// <summary>
        /// 根据分页筛选获取符合条件的对象集合
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="pageIndex">分页页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="recordCount">总记录数。为-1时，将从数据库获取该值</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回对象集合</returns>
        public virtual List<Tm> _GetList<Tm>(int? pageIndex, int? pageSize, ref int? recordCount, string fields, string filter, string sorts, List<DBParameter> parameters) where Tm : BaseModel, new()
        {
            string tableName = InitTableName<Tm>();
            return _GetList<Tm>(pageIndex, pageSize, ref recordCount, fields, filter, sorts, tableName, parameters);
        }

        /// <summary>
        /// 从指定表中获取全部对象
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="tableName">表名</param>
        /// <returns>返回对象集合</returns>
        public virtual List<Tm> _GetList<Tm>(string tableName) where Tm : BaseModel, new()
        {
            string sql = "SELECT * FROM " + SqlHelper.FormatDB(tableName, this.DBType) + "";
            return _GetList<Tm>(sql, null);
        }

        /// <summary>
        /// 获取符合条件的对象集合
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="tableName">表名</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回对象集合</returns>
        public virtual List<Tm> _GetList<Tm>(string fields, string filter, string sorts, string tableName, List<DBParameter> parameters) where Tm : BaseModel, new()
        {

            filter = BaseHelper.GetDefaultValue(filter, "1=1");

            fields = BaseHelper.GetDefaultValue(fields, "*");

            if (parameters == null && string.IsNullOrEmpty(tableName))
            {
                tableName = InitTableName<Tm>();
            }

            string sql;

            if (string.IsNullOrEmpty(sorts))
                sql = SqlHelper.FormatSql(fields, filter, tableName, this.DBType);
            else
                sql = SqlHelper.FormatSql(fields, filter, sorts, tableName, this.DBType);
            return _GetList<Tm>(sql, parameters);
        }

        /// <summary>
        /// 以分页形式获取数据集合
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="recordCount">总记录数。为-1时，将从数据库获取该值</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="tableName">表名</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回对象集合</returns>
        public virtual List<Tm> _GetList<Tm>(int? pageIndex, int? pageSize, ref int? recordCount, string fields, string filter, string sorts, string tableName, List<DBParameter> parameters) where Tm : BaseModel, new()
        {
            Tm model = new Tm();
            string primaryKey = model.GetPrimaryKey();

            SqlHelper.FormatArg(ref pageIndex, ref pageSize, ref recordCount, ref fields, ref filter, ref sorts, primaryKey);

            if (parameters == null) parameters = new List<DBParameter>();

            DBParameter parameter = new DBParameter("", "RecordCount", recordCount);
            parameter.Size = 4;
            parameter.DbType = DbType.Int32;
            parameter.Direction = ParameterDirection.Output;

            parameters.Add(parameter);

            parameters = InitDataParameter(parameters, pageIndex, pageSize);

            string sql = SqlHelper.GetPageViewSql(pageIndex, pageSize, fields, filter, sorts, tableName, primaryKey, this.DBType);

            List<Tm> list = null;

            DbParameterCollection outParameters;

            using (IDataReader dr = ExecuteReader(parameters, sql, out outParameters))
            {
                list = _GetList<Tm>(dr);
            }


            recordCount = Convert.ToInt32(outParameters["@RecordCount"].Value, CultureInfo.InvariantCulture);

            return list;

        }

        #endregion

        #region Get

        /// <summary>
        /// 通过IDataReader获取对象
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="dr">IDataReader</param>
        /// <returns>返回对象</returns>
        public virtual Tm _GetModel<Tm>(IDataReader dr) where Tm : BaseModel, new()
        {
            SmartDataReader smartdr = new SmartDataReader(dr);
            Tm model = new Tm();

            model.OnLoading();

            string fieldName;

            for (int i = 0; i < dr.FieldCount; i++)
            {
                fieldName = CString.MakeCamel(dr.GetName(i));
                object value = SmartReader.GetSmartValue(smartdr, fieldName);
                OnFieldSetting(fieldName, value);
                CModel.SetProValue(model, fieldName, value);
                OnFieldSetted(fieldName, value);
            }

            model.OnLoaded();

            return model;
        }

        /// <summary>
        /// 通过DataRow 获取对应对象
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="dr">DataRow</param>
        /// <returns>返回对象</returns>
        public virtual Tm _GetModel<Tm>(DataRow dr) where Tm : BaseModel, new()
        {
            SmartDataRow sdrv = new SmartDataRow(dr);
            Tm model = new Tm();
            model.OnLoading();

            string fieldName;

            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                fieldName = CString.MakeCamel(dr.Table.Columns[i].ColumnName);

                object value = SmartReader.GetSmartValue(sdrv, fieldName);

                OnFieldSetting(fieldName, value);
                CModel.SetProValue(model, fieldName, value);
                OnFieldSetted(fieldName, value);
            }

            model.OnLoaded();

            return model;
        }

        /// <summary>
        /// 根据DataRowView获取对应对象
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="dv">DataRowView</param>
        /// <returns>返回对应对象</returns>
        public virtual Tm _GetModel<Tm>(DataRowView dv) where Tm : BaseModel, new()
        {
            SmartDataRowView sdrv = new SmartDataRowView(dv);
            Tm model = new Tm();
            model.OnLoading();

            string fieldName;

            for (int i = 0; i < dv.DataView.Table.Columns.Count; i++)
            {
                fieldName = CString.MakeCamel(dv.DataView.Table.Columns[i].ColumnName);
                object value = SmartReader.GetSmartValue(sdrv, fieldName);
                OnFieldSetting(fieldName, value);
                CModel.SetProValue(model, fieldName, value);
                OnFieldSetted(fieldName, value);
            }

            model.OnLoaded();
            return model;
        }

        /// <summary>
        /// 通过IDataReader获取对象集合
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="dr">IDataReader</param>
        /// <returns>返回对象集合</returns>
        public virtual List<Tm> _GetList<Tm>(IDataReader dr) where Tm : BaseModel, new()
        {
            List<Tm> list = new List<Tm>();

            using (dr)
            {
                while (dr.Read())
                {
                    list.Add(_GetModel<Tm>(dr));
                }
            }

            return list;
        }

        /// <summary>
        /// 通过DataTable 获取 对象集合
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="dt">DataTable</param>
        /// <returns>返回对象集合</returns>
        public virtual List<Tm> _GetList<Tm>(DataTable dt) where Tm : BaseModel, new()
        {
            List<Tm> list = new List<Tm>();

            foreach (DataRow r in dt.Rows)
            {
                list.Add(_GetModel<Tm>(r));
            }

            return list;
        }

        /// <summary>
        /// 通过DataView获取对象集合
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="dv">DataView</param>
        /// <returns>返回对象集合</returns>
        public virtual List<Tm> _GetList<Tm>(DataView dv) where Tm : BaseModel, new()
        {
            List<Tm> list = new List<Tm>();

            foreach (DataRow r in dv)
            {
                list.Add(_GetModel<Tm>(r));
            }

            return list;
        }

        /// <summary>
        /// 获取符合条件的对象
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="sqlInfo">封装的筛选信息</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回对象</returns>
        public virtual Tm _GetModel<Tm>(SqlInfo sqlInfo, List<DBParameter> parameters) where Tm : BaseModel, new()
        {
            sqlInfo = InitSqlInfo<SqlInfo>(sqlInfo);

            return _GetModel<Tm>(sqlInfo.Fields, sqlInfo.Filter, sqlInfo.Sorts, parameters);
        }

        /// <summary>
        /// 获取符合条件的对象集合
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="sqlInfo">封装的筛选信息</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回对象集合</returns>
        public virtual List<Tm> _GetList<Tm>(SqlInfo sqlInfo, List<DBParameter> parameters) where Tm : BaseModel, new()
        {
            sqlInfo = InitSqlInfo<SqlInfo>(sqlInfo);
            return _GetList<Tm>(sqlInfo.Fields, sqlInfo.Filter, sqlInfo.Sorts, parameters);
        }

        /// <summary>
        /// 根据分页获取符合条件的对象集合
        /// </summary>
        /// <typeparam name="Tm">对象泛型类型</typeparam>
        /// <param name="pageInfo">封装的分页数据筛选信息</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回对象集合</returns>
        public virtual List<Tm> _GetList<Tm>(PageInfo pageInfo, List<DBParameter> parameters) where Tm : BaseModel, new()
        {
            pageInfo = InitSqlInfo<PageInfo>(pageInfo);

            int? re = pageInfo.RecordCount;

            List<Tm> dt = _GetList<Tm>(pageInfo.PageIndex, pageInfo.PageSize, ref re, pageInfo.Fields, pageInfo.Filter, pageInfo.Sorts, parameters);

            pageInfo.RecordCount = re.Value;

            return dt;
        }

        #endregion

        #endregion

        #region 私有方法

        private void InitDatabase<T>(string commandText, CommandType commandType, Database db, ref DbCommand dbCommand, List<T> parameters) where T : IDataParameter, IDbDataParameter
        {
            if (commandType == CommandType.StoredProcedure)
            {
                dbCommand = db.GetStoredProcCommand(commandText);
            }
            else
            {
                dbCommand = db.GetSqlStringCommand(commandText);

            }
            dbCommand.CommandTimeout = TimeOut;

            if (parameters != null)
            {
                foreach (T dbParameter in parameters)
                {
                    if (dbParameter.Direction != ParameterDirection.Input)
                    {
                        db.AddOutParameter(dbCommand, dbParameter.ParameterName, dbParameter.DbType, dbParameter.Size);
                    }
                    else
                    {
                        db.AddInParameter(dbCommand, dbParameter.ParameterName, dbParameter.DbType, dbParameter.Value);
                    }
                }
            }
        }

        private void GetParameterResultValue<T>(Database db, DbCommand dbCommand, List<T> parameters) where T : IDataParameter, IDbDataParameter
        {
            if (parameters != null)
            {
                foreach (T dbParameter in parameters)
                {
                    if (dbParameter.Direction != ParameterDirection.Input)
                    {
                        dbParameter.Value = db.GetParameterValue(dbCommand, dbParameter.ParameterName);
                    }
                }
            }
        }

        protected virtual List<DBParameter> InitDataParameter(List<DBParameter> parameters, int? pageIndex, int? pageSize)
        {
            if (parameters == null)
                parameters = new List<DBParameter>();
            DBParameter top = new DBParameter();
            top.DbType = DbType.Int32;
            top.ParameterName = "Top";
            top.Value = pageIndex * pageSize;
            parameters.Add(top);

            DBParameter size = new DBParameter();
            size.DbType = DbType.Int32;
            size.ParameterName = "pageSize";
            size.Value = pageSize;
            parameters.Add(size);

            return parameters;
        }

        protected virtual List<DbParameter> InitDataParameter(List<DbParameter> parameters, int? pageIndex, int? pageSize)
        {
            if (parameters == null)
                parameters = new List<DbParameter>();
            DbCommand dbCommand = DB.GetSqlStringCommand("");
            DbParameter top = dbCommand.CreateParameter();
            top.DbType = DbType.Int32;
            top.ParameterName = "Top";
            top.Value = pageIndex * pageSize;
            parameters.Add(top);

            DbParameter size = dbCommand.CreateParameter();
            size.DbType = DbType.Int32;
            size.ParameterName = "pageSize";
            size.Value = pageSize;
            parameters.Add(size);

            dbCommand.Dispose();

            return parameters;
        }

        #endregion

        protected virtual string InitTableName<Tm>() where Tm : BaseModel, new()
        {
            return BaseHelper.GetTableName<Tm>();
        }

        protected virtual string InitTableName(BaseModel model)
        {
            return BaseHelper.GetTableName(model);
        }

        #region IDalExecute 成员

        /// <summary>
        /// 执行SQL语句，返回IDataReader
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <param name="parameters">参数集合</param>
        /// <param name="outParameters">返回的参数集合</param>
        /// <returns>返回IDataReader</returns>
        public virtual IDataReader ExecuteReader(string commandText, List<DbParameter> parameters, out DbParameterCollection outParameters)
        {
            return this.ExecuteReader(commandText, CommandType.Text, parameters, out outParameters);
        }

        /// <summary>
        /// 执行SQL语句或存储过程，返回IDataReader
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <param name="commandType">命令字符串类型</param>
        /// <param name="parameters">参数集合</param>
        /// <param name="outParameters">返回的参数集合</param>
        /// <returns>返回IDataReader</returns>
        public virtual IDataReader ExecuteReader(string commandText, CommandType commandType, List<DbParameter> parameters, out DbParameterCollection outParameters)
        {
            Database db = DB;
            DbCommand dbCommand = null;
            IDataReader result;

            InitDatabase<DbParameter>(commandText, commandType, db, ref dbCommand, parameters);

            if (this.Transaction == null)
            {
                using (dbCommand)
                {
                    result = db.ExecuteReader(dbCommand);
                    outParameters = dbCommand.Parameters;
                    dbCommand.Dispose();
                }
            }
            else
            {
                result = db.ExecuteReader(dbCommand, this.Transaction);
                outParameters = dbCommand.Parameters;
            }

            return result;
        }

        /// <summary>
        /// 执行SQL语句或存储过程，返回IDataReader
        /// </summary>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <param name="parameters">参数集合</param>
        /// <param name="commandType">命令字符串类型</param>
        /// <param name="outParameters">返回的参数集合</param>
        /// <returns>返回IDataReader</returns>
        public virtual IDataReader ExecuteReader(string commandText, List<DBParameter> parameters, CommandType commandType, out DbParameterCollection outParameters)
        {
            Database db = DB;
            DbCommand dbCommand = null;
            IDataReader result;

            InitDatabase<DBParameter>(commandText, commandType, db, ref dbCommand, parameters);

            if (this.Transaction == null)
            {
                using (dbCommand)
                {
                    result = db.ExecuteReader(dbCommand);
                    outParameters = dbCommand.Parameters;
                    dbCommand.Dispose();
                }
            }
            else
            {
                result = db.ExecuteReader(dbCommand, this.Transaction);
                outParameters = dbCommand.Parameters;
            }

            return result;
        }

        /// <summary>
        /// 执行SQL语句，返回IDataReader
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="commandText">SQL语句或存储过程名称</param>
        /// <param name="outParameters">返回的参数集合</param>
        /// <returns>返回IDataReader</returns>
        public virtual IDataReader ExecuteReader(List<DBParameter> parameters, string commandText, out DbParameterCollection outParameters)
        {
            return this.ExecuteReader(commandText, parameters, CommandType.Text, out outParameters);
        }

        #endregion

        #region IEDal 成员

        public virtual int Delete(Hashtable list)
        {
            int result = 0;

            if (list.Count > 0)
            {
                string tableName = "";

                foreach (DictionaryEntry de in list)
                {
                    tableName = InitTableName(((BaseModel)(de.Value)));
                    break;
                }
                Delete(list, tableName);
            }

            return result;
        }

        public virtual int Delete(Hashtable list, string tableName)
        {
            int result = 0;

            if (list.Count > 0)
            {
                string ids = "";

                BaseModel model = null;

                foreach (DictionaryEntry de in list)
                {
                    model = ((BaseModel)(de.Value));
                    ids = ids + "'" + de.Key.ToString() + "',";
                }

                ids = ids.Substring(0, ids.Length - 1);


                result = ExecuteNonQuery("DELETE FROM " + tableName + " WHERE " + model.GetPrimaryKey() + " IN (" + ids + ")");
            }

            return result;
        }

        public virtual int Insert(Hashtable list, string fields)
        {
            int result = 0;

            if (list.Count > 0)
            {
                string tableName = "";

                foreach (DictionaryEntry de in list)
                {
                    tableName = InitTableName(((BaseModel)(de.Value)));
                    break;
                }
                Insert(list, fields, tableName);
            }

            return result;
        }

        public virtual int Insert(Hashtable list, string fields, string tableName)
        {
            int result = list.Count;

            if (Transaction == null)
            {
                this.OpenTransaction();
                try
                {
                    foreach (DictionaryEntry de in list)
                    {
                        this.Insert((BaseModel)(de.Value), fields, tableName);
                    }
                    this.Transaction.Commit();
                }
                catch
                {
                    this.Transaction.Rollback();
                    result = 0;
                }
                finally
                {
                    this.Transaction.Dispose();
                    this.Transaction = null;
                }
            }
            else
            {

                foreach (DictionaryEntry de in list)
                {
                    this.Insert((BaseModel)(de.Value), fields, tableName);
                }
            }
            return result;
        }

        public virtual int InsertOrUpdate(Hashtable list, string fields)
        {
            int result = 0;

            if (list.Count > 0)
            {
                string tableName = "";

                foreach (DictionaryEntry de in list)
                {
                    tableName = InitTableName(((BaseModel)(de.Value)));
                    break;
                }
                InsertOrUpdate(list, fields, tableName);
            }

            return result;
        }

        public virtual int InsertOrUpdate(Hashtable list, string fields, string tableName)
        {
            int result = list.Count;

            if (Transaction == null)
            {
                this.OpenTransaction();
                try
                {
                    foreach (DictionaryEntry de in list)
                    {
                        this.InsertOrUpdate((BaseModel)(de.Value), fields, tableName);
                    }
                    this.Transaction.Commit();
                }
                catch
                {
                    this.Transaction.Rollback();
                    result = 0;
                }
                finally
                {
                    this.Transaction.Dispose();
                    this.Transaction = null;
                }
            }
            else
            {
                foreach (DictionaryEntry de in list)
                {
                    this.InsertOrUpdate((BaseModel)(de.Value), fields, tableName);
                }
            }
            return result;
        }

        public virtual int Update(Hashtable list, string fields)
        {
            int result = 0;

            if (list.Count > 0)
            {
                string tableName = "";

                foreach (DictionaryEntry de in list)
                {
                    tableName = InitTableName(((BaseModel)(de.Value)));
                    break;
                }
                Update(list, fields, tableName);
            }

            return result;
        }

        public virtual int Update(Hashtable list, string fields, string tableName)
        {
            int result = list.Count;

            if (Transaction == null)
            {
                this.OpenTransaction();
                try
                {
                    foreach (DictionaryEntry de in list)
                    {
                        this.Update((BaseModel)(de.Value), fields, tableName);
                    }
                    this.Transaction.Commit();
                }
                catch
                {
                    this.Transaction.Rollback();
                    result = 0;
                }
                finally
                {
                    this.Transaction.Dispose();
                    this.Transaction = null;
                }
            }
            else
            {
                foreach (DictionaryEntry de in list)
                {
                    this.Update((BaseModel)(de.Value), fields, tableName);
                }
            }
            return result;
        }


        #endregion

        #region IEDal 成员


        public virtual int _Add<Tm>(Dictionary<string, Tm> list, string fields) where Tm : BaseModel, new()
        {
            int result = 0;

            if (list.Count > 0)
            {
                result = _Add<Tm>(list, fields, BaseHelper.GetTableName<Tm>());
            }

            return result;
        }

        public virtual int _Add<Tm>(Dictionary<string, Tm> list, string fields, string tableName) where Tm : BaseModel, new()
        {
            int result = list.Count;

            if (Transaction == null)
            {
                this.OpenTransaction();
                try
                {
                    foreach (KeyValuePair<string, Tm> de in list)
                    {
                        this.Insert(de.Value, fields, tableName);
                    }
                    this.Transaction.Commit();
                }
                catch
                {
                    this.Transaction.Rollback();
                    result = 0;
                }
                finally
                {
                    this.Transaction.Dispose();
                    this.Transaction = null;
                }
            }
            else
            {
                foreach (KeyValuePair<string, Tm> de in list)
                {
                    this.Insert(de.Value, fields, tableName);
                }
            }
            return result;
        }

        public virtual int _Remove<Tm>(Dictionary<string, Tm> list) where Tm : BaseModel, new()
        {
            return _Remove<Tm>(list, BaseHelper.GetTableName<Tm>());
        }

        public virtual int _Remove<Tm>(Dictionary<string, Tm> list, string tableName) where Tm : BaseModel, new()
        {
            int result = 0;

            if (list.Count > 0)
            {
                string ids = "";

                foreach (string id in list.Keys)
                {
                    ids = ids + "'" + id + "',";
                }

                ids = ids.Substring(0, ids.Length - 1);

                Tm tm = new Tm();

                result = ExecuteNonQuery("DELETE FROM " + tableName + " WHERE " + tm.GetPrimaryKey() + " IN (" + ids + ")");
            }

            return result;
        }

        public virtual int _Modify<Tm>(Dictionary<string, Tm> list, string fields) where Tm : BaseModel, new()
        {
            int result = 0;

            if (list.Count > 0)
            {
                result = _Modify<Tm>(list, fields, BaseHelper.GetTableName<Tm>());
            }

            return result;
        }

        public virtual int _Modify<Tm>(Dictionary<string, Tm> list, string fields, string tableName) where Tm : BaseModel, new()
        {
            int result = list.Count;

            if (Transaction == null)
            {
                this.OpenTransaction();
                try
                {
                    foreach (KeyValuePair<string, Tm> de in list)
                    {
                        this.Update(de.Value, fields, tableName);
                    }
                    this.Transaction.Commit();
                }
                catch
                {
                    this.Transaction.Rollback();
                    result = 0;
                }
                finally
                {
                    this.Transaction.Dispose();
                    this.Transaction = null;
                }
            }
            else
            {
                foreach (KeyValuePair<string, Tm> de in list)
                {
                    this.Update(de.Value, fields, tableName);
                }
            }
            return result;
        }

        public virtual int _AddOrModify<Tm>(Dictionary<string, Tm> list, string fields) where Tm : BaseModel, new()
        {
            int result = 0;

            if (list.Count > 0)
            {
                result = _AddOrModify<Tm>(list, fields, BaseHelper.GetTableName<Tm>());
            }

            return result;
        }

        public virtual int _AddOrModify<Tm>(Dictionary<string, Tm> list, string fields, string tableName) where Tm : BaseModel, new()
        {
            int result = list.Count;

            if (Transaction == null)
            {
                this.OpenTransaction();
                try
                {
                    foreach (KeyValuePair<string, Tm> de in list)
                    {
                        this.InsertOrUpdate(de.Value, fields, tableName);
                    }
                    this.Transaction.Commit();
                }
                catch
                {
                    this.Transaction.Rollback();
                    result = 0;
                }
                finally
                {
                    this.Transaction.Dispose();
                    this.Transaction = null;
                }
            }
            else
            {
                foreach (KeyValuePair<string, Tm> de in list)
                {
                    this.InsertOrUpdate(de.Value, fields, tableName);
                }
            }
            return result;
        }

        public virtual Dictionary<string, Tm> _GetDicList<Tm>() where Tm : BaseModel, new()
        {
            return _GetDicList<Tm>(null, null, null, null);

        }

        public virtual Dictionary<string, Tm> _GetDicList<Tm>(string sql, List<DBParameter> parameters) where Tm : BaseModel, new()
        {
            IDataReader reader = ExecuteReader(parameters, sql);
            return _GetDicList<Tm>(reader);
        }

        public virtual Dictionary<string, Tm> _GetDicList<Tm>(string fields, string filter, string sorts, List<DBParameter> parameters) where Tm : BaseModel, new()
        {
            return _GetDicList<Tm>(null, null, null, BaseHelper.GetTableName<Tm>(), null);
        }

        public virtual Dictionary<string, Tm> _GetDicList<Tm>(int? pageIndex, int? pageSize, ref int? recordCount, string fields, string filter, string sorts, List<DBParameter> parameters) where Tm : BaseModel, new()
        {
            Tm tm = new Tm();
            string primaryKey = tm.GetPrimaryKey();

            SqlHelper.FormatArg(ref pageIndex, ref pageSize, ref recordCount, ref fields, ref filter, ref sorts, primaryKey);

            if (parameters == null) parameters = new List<DBParameter>();

            DBParameter parameter = new DBParameter("", "@RecordCount", recordCount);
            parameter.Size = 4;
            parameter.DbType = DbType.Int32;
            parameter.Direction = ParameterDirection.Output;

            parameters.Add(parameter);

            parameters = InitDataParameter(parameters, pageIndex, pageSize);

            string sql = SqlHelper.GetPageViewSql(pageIndex, pageSize, fields, filter, sorts, BaseHelper.GetTableName<Tm>(), primaryKey, this.DBType);

            Dictionary<string, Tm> result = null;


            DbParameterCollection outParameters;

            using (IDataReader dr = ExecuteReader(parameters, sql, out outParameters))
            {
                result = _GetDicList<Tm>(dr);
            }

            recordCount = Convert.ToInt32(outParameters["@RecordCount"].Value, CultureInfo.InvariantCulture);

            return result;

        }

        public virtual Dictionary<string, Tm> _GetDicList<Tm>(string tableName) where Tm : BaseModel, new()
        {
            return _GetDicList<Tm>(null, null, null, tableName, null);
        }

        public virtual Dictionary<string, Tm> _GetDicList<Tm>(string fields, string filter, string sorts, string tableName, List<DBParameter> parameters) where Tm : BaseModel, new()
        {
            filter = BaseHelper.GetDefaultValue(filter, "1=1");

            fields = BaseHelper.GetDefaultValue(fields, "*");
            if (parameters == null && string.IsNullOrEmpty(tableName))
            {
                tableName = InitTableName<Tm>();
            }
            string sql;

            if (string.IsNullOrEmpty(sorts))
                sql = SqlHelper.FormatSql(fields, filter, tableName, this.DBType);
            else
                sql = SqlHelper.FormatSql(fields, filter, sorts, tableName, this.DBType);
            return _GetDicList<Tm>(sql, parameters);
        }

        public virtual Dictionary<string, Tm> _GetDicList<Tm>(int? pageIndex, int? pageSize, ref int? recordCount, string fields, string filter, string sorts, string tableName, List<DBParameter> parameters) where Tm : BaseModel, new()
        {
            throw new NotImplementedException();
        }

        public virtual Dictionary<string, Tm> _GetDicList<Tm>(IDataReader dr) where Tm : BaseModel, new()
        {
            Dictionary<string, Tm> result = new Dictionary<string, Tm>();

            using (dr)
            {
                while (dr.Read())
                {
                    Tm tm = _GetModel<Tm>(dr);
                    result.Add(CModel.GetProValue<string>(tm.GetPrimaryKey(), tm), tm);
                }

            }
            return result;
        }

        public virtual Dictionary<string, Tm> _GetDicList<Tm>(DataTable dt) where Tm : BaseModel, new()
        {
            Dictionary<string, Tm> result = new Dictionary<string, Tm>();


            foreach (DataRow r in dt.Rows)
            {
                Tm tm = _GetModel<Tm>(r);
                result.Add(CModel.GetProValue<string>(tm.GetPrimaryKey(), tm), tm);

            }
            return result;
        }

        public virtual Dictionary<string, Tm> _GetDicList<Tm>(DataView dv) where Tm : BaseModel, new()
        {
            Dictionary<string, Tm> result = new Dictionary<string, Tm>();


            foreach (DataRowView dr in dv)
            {
                Tm tm = _GetModel<Tm>(dr);
                result.Add(CModel.GetProValue<string>(tm.GetPrimaryKey(), tm), tm);

            }
            return result;
        }

        public virtual Dictionary<string, Tm> _GetDicList<Tm>(SqlInfo sqlInfo, List<DBParameter> parameters) where Tm : BaseModel, new()
        {
            sqlInfo = InitSqlInfo<SqlInfo>(sqlInfo);
            return _GetDicList<Tm>(sqlInfo.Fields, sqlInfo.Filter, sqlInfo.Sorts, parameters);
        }

        public virtual Dictionary<string, Tm> _GetDicList<Tm>(PageInfo pageInfo, List<DBParameter> parameters) where Tm : BaseModel, new()
        {
            pageInfo = InitSqlInfo<PageInfo>(pageInfo);

            int? re = pageInfo.RecordCount;

            Dictionary<string, Tm> dt = _GetDicList<Tm>(pageInfo.PageIndex, pageInfo.PageSize, ref re, pageInfo.Fields, pageInfo.Filter, pageInfo.Sorts, parameters);

            pageInfo.RecordCount = re.Value;

            return dt;
        }

        #endregion

        public virtual void OnFieldSetting(string fieldName, object fieldValue)
        {

        }

        public virtual void OnFieldSetted(string fieldName, object fieldValue)
        {

        }


        public List<Tm> _GetTop<Tm>(int count) where Tm : BaseModel, new()
        {
            string tableName = InitTableName<Tm>();
            return _GetTop<Tm>(tableName, count);

        }

        public List<Tm> _GetTop<Tm>(int count, string sorts) where Tm : BaseModel, new()
        {
            string tableName = InitTableName<Tm>();
            return _GetTop<Tm>(tableName, count, sorts);
        }

        public List<Tm> _GetTop<Tm>(int count, string fields, string filter, string sorts, List<DBParameter> parameters) where Tm : BaseModel, new()
        {
            string tableName = InitTableName<Tm>();
            return _GetTop<Tm>(tableName, count, fields, filter, sorts, parameters);
        }

        public List<Tm> _GetTop<Tm>(string tableName, int count) where Tm : BaseModel, new()
        {
            return _GetTop<Tm>(tableName, count, "");
        }

        public List<Tm> _GetTop<Tm>(string tableName, int count, string sorts) where Tm : BaseModel, new()
        {
            return _GetTop<Tm>(tableName, count, "", "", sorts, null);
        }

        public List<Tm> _GetTop<Tm>(string tableName, int count, string fields, string filter, string sorts, List<DBParameter> parameters) where Tm : BaseModel, new()
        {
            filter = BaseHelper.GetDefaultValue(filter, "1=1");

            fields = BaseHelper.GetDefaultValue(fields, "*");
            if (parameters == null && string.IsNullOrEmpty(tableName))
            {
                tableName = InitTableName<Tm>();
            }
            string sql;

            if (string.IsNullOrEmpty(sorts))
                sql = SqlHelper.FormatSql(fields, filter, tableName, this.DBType, count);
            else
                sql = SqlHelper.FormatSql(fields, filter, sorts, tableName, this.DBType, count);
            return _GetList<Tm>(sql, parameters);
        }

        public int _InsertOrUpdateBySQL<Tm>(Tm model) where Tm : BaseModel, new()
        {
            return _InsertOrUpdateBySQL<Tm>(model, "");
        }

        public int _InsertOrUpdateBySQL<Tm>(List<Tm> list) where Tm : BaseModel, new()
        {
            return _InsertOrUpdateBySQL(list, "");
        }

        public int _InsertOrUpdateBySQL<Tm>(Tm model, string fields) where Tm : BaseModel, new()
        {
            string tableName = InitTableName<Tm>();
            return _InsertOrUpdateBySQL(model, fields, tableName);
        }

        public int _InsertOrUpdateBySQL<Tm>(List<Tm> list, string fields) where Tm : BaseModel, new()
        {
            string tableName = InitTableName<Tm>();
            return _InsertOrUpdateBySQL(list, fields, tableName);
        }

        public int _InsertOrUpdateBySQL<Tm>(Tm model, string fields, string tableName) where Tm : BaseModel, new()
        {
            model.OnSubmiting();
            List<DBParameter> parameters = new List<DBParameter>();
            //string sql = SqlHelper.GetAddSql(model, fields, tableName, parameters, this.DBType);
            string sql = SqlHelper.GetInsertOrUpdateSql(model, fields, tableName, parameters, DBType);
            int result = 0;
            if (!string.IsNullOrEmpty(sql))
                result = ExecuteNonQuery(parameters, sql);
            model.OnSubmited();

            return result;
        }

        public int _InsertOrUpdateBySQL<Tm>(List<Tm> list, string fields, string tableName) where Tm : BaseModel, new()
        {
            int result = list.Count;

            if (Transaction == null)
            {
                this.OpenTransaction();
                try
                {
                    foreach (Tm model in list)
                    {
                        this._InsertOrUpdateBySQL(model, fields, tableName);
                    }
                    this.Transaction.Commit();
                }
                catch
                {
                    this.Transaction.Rollback();
                    result = 0;
                }
                finally
                {
                    this.Transaction.Dispose();
                    this.Transaction = null;
                }
            }
            else
            {
                foreach (Tm model in list)
                {
                    this._InsertOrUpdateBySQL(model, fields, tableName);
                }
            }
            return result;
        }
    }
}